package xzy.com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import xzy.com.config.jwt.annotation.PassToken;
import xzy.com.entity.domain.Menu;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.MenuService;

@RestController
@PassToken
@RequestMapping("/api/menu")
public class MenuController {
	@Autowired
	private MenuService menuService;

	// 动态菜单
	@PassToken
	@GetMapping("/boardList")
	public ResponseBean boardList() {
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> boardList = menuService.boardList();
		map.put("boardList", boardList);
		return new ResponseBean(HttpStatus.OK.value(), "动态菜单请求成功！", map);
	}

	@PassToken
	@PostMapping("/save")//添加操作
	public ResponseBean add(@RequestBody Menu menu) {
		if (menu != null) {
			Boolean result = menuService.save(menu);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "添加成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "添加数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "数据不能为空", null);
	}

	@PassToken
	@DeleteMapping("/delete/{menuId}")//根据id删除操作
	public ResponseBean delete(@PathVariable("menuId") Long id) {
		if (null != null) {
			boolean isDel = menuService.removeById(id);
			if (isDel) {
				return new ResponseBean(HttpStatus.OK.value(), "删除数据成功!", isDel);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除数据失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 分页处理
	@PassToken
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Menu> page = new Page<>(pages, size);
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("menu_id");
		Page<Menu> pages1 = menuService.page(page, queryWrapper);

		IPage<Menu> menuPage = menuService.findParentMenu(pages1, queryWrapper);
		// 查询父级菜单
		List<Menu> parentMenu = menuService.queryParentMenu();
		Map<String, Object> map = new HashMap<>();
		map.put("list", menuPage.getRecords());
		map.put("recordTotal", menuPage.getTotal());
		map.put("pageTotal", menuPage.getPages());
		map.put("parentMenu", parentMenu);
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 搜索查询分页处理
	@PassToken
	@GetMapping("/search")
	public ResponseBean searchListPages(@RequestParam("menuName") String name, @RequestParam("pages") Integer pages,
			@RequestParam("size") Integer size) {
		Page<Menu> page = new Page<>(pages, size);
		QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("menu_id");
		if (null != name) {
			queryWrapper.like("menu_name", name);
		}
		Page<Menu> menuPage = menuService.page(page, queryWrapper);
		// IPage<Menu> menuPage = menuService.findParentMenu(pages1, queryWrapper);
		// 查询所有父级菜单
		List<Menu> parentMenu = menuService.queryParentMenu();

		Map<String, Object> map = new HashMap<>();
		map.put("list", menuPage.getRecords());
		map.put("recordTotal", menuPage.getTotal());
		map.put("pageTotal", menuPage.getPages());
		map.put("parentMenu", parentMenu);
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 修改
	@PassToken
	@PutMapping("/update")
	public ResponseBean updateUser(@RequestBody Menu menu) {
		if (null != menu) {// 核验数据
			boolean result = menuService.updateById(menu);
			if (result) {
				new ResponseBean(HttpStatus.OK.value(), "修改成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "修改失败", null);
	}

	@PassToken
	@DeleteMapping("/delete")//删除
	public ResponseBean delete(@RequestParam("menuId") Integer id) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
		if (null != id) {
			boolean result = menuService.removeById(id);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

	// 传入的json数组key=list，为一组整数索引
	@PassToken
	@DeleteMapping("/deleteAll")
	public ResponseBean deleteAll(@RequestBody JSONObject jsonObject) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "批量删除失败", null);
		JSONArray jsonArray = jsonObject.getJSONArray("list");
		List<Integer> idList = jsonArray.toJavaList(Integer.class);
		if (!idList.isEmpty()) {
			boolean result = menuService.removeByIds(idList);
			if (result) {
				return new ResponseBean(HttpStatus.OK.value(), "删除成功", true);
			} else {
				return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
			}
		}
		return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
	}

}
